package com.guestmanage.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guestmanage.bean.ChannelArgs;
import com.guestmanage.bean.ChannelRecordVO;
import com.guestmanage.service.ChannelRecordService;
import com.guestmanage.servlet.util.BaseAction;
import com.guestmanage.servlet.util.PageBean;

public class ChannelRecordServlet extends HttpServlet{
	
	private static final long serialVersionUID = -631452077064709725L;
	
	private ChannelRecordService crSer=new ChannelRecordService();
	
	/**
	 * 截取页面传来的时间
	 * @param dateStr
	 * @param type 如果是0，返回月份，是1返回天数
	 * @return
	 */
	private int getonths(String dateStr,int type){
		try {
			if(type==0)
				return Integer.parseInt(dateStr.split("-")[1]);
			if(type==1)
				return Integer.parseInt(dateStr.split("-")[2]);
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BaseAction base=new BaseAction(request, response);
		
		String from=base.getParameterString("from");
		int timerStr=base.getParameterInt("timerStr");
		
		String port=base.getParameterString("port");
		String order=base.getParameterString("order");
		String spCode=base.getParameterString("spCode");
		
		String city=base.getParameterString("city");
		if(city!=null)
			base.setSessionAttribute("channel_city", city);
		if(city==null)
			city=(String)base.getSessionAttribute("channel_city");
		
		int months=this.getonths(from, 0);
		
		ChannelArgs ca=new ChannelArgs();
		ca.setBody(order);
		ca.setCity(city);
		ca.setDesc(port);
		ca.setMonths(months);
		ca.setStype(spCode);
		ca.setFrom(from);
		
		if(timerStr==24){
			ca.setTimerStr(00);
		}
		ca.setTimerStr(timerStr);
		
		int page=base.getParameterInt("page");
		
		List<ChannelRecordVO> crvo_list=new ArrayList<ChannelRecordVO>();
		PageBean<ChannelRecordVO> pagebean=null;
		
		if(crSer.getMonths()==getonths(from,0)&&crSer.getDay()==getonths(from,1)){
			int dayCount=crSer.getCountDays(ca);
			pagebean=new PageBean<ChannelRecordVO>(page,dayCount);
			crvo_list=crSer.queryForCRVODays(ca, pagebean.getPageSize(), pagebean.getPageNow());
		}else{
			int monthsCount=crSer.getCountMonths(ca);
			pagebean=new PageBean<ChannelRecordVO>(page,monthsCount);
			crvo_list=crSer.queryCRVOMonths(ca, pagebean.getPageSize(), pagebean.getPageNow());
		}
		

		pagebean.setResult(crvo_list);
		pagebean.setUrl(request.getContextPath()+"/ChannelRecordServlet");
		
		base.setAttribute("pagebean", pagebean);
		
		String where="&timerStr="+timerStr+"&from="+from+"&months="+months+"&port="+port+"&order="+order+"&spCode="+spCode;
		
		base.setAttribute("where", where);
		base.setAttribute("ca", ca);
		
		base.forward("guestManage/monitor/channelRecord.jsp");
	}
}
